package com.rtx.wms.std.validation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.common.bean.Codelkup;
import com.rtx.wms.std.common.bean.PlDb;
import com.rtx.wms.std.common.mapper.CodelkupMapper;
import com.rtx.wms.std.common.mapper.CommonMapper;
import com.rtx.wms.std.erpconfig.bean.RtxErpConfig;
import com.rtx.wms.std.erpconfig.mapper.RtxErpConfigMapper;
import com.rtx.wms.std.sku.bean.po.Sku;
import com.rtx.wms.std.sku.mapper.SkuMapper;

/**
 * 基础验证
 */
public class BaseValidation {

    @Autowired
    protected SkuMapper skuMapper;

    @Autowired
    protected CodelkupMapper codelkupMapper;

    @Autowired
    protected CommonMapper commonMapper;
    
    @Autowired
    protected RtxErpConfigMapper rtxErpConfigMapper;

    /**
     * 验证工厂库存地点
     * @param whseid
     * @param storerkey
     * @param sku
     * @param isExists
     * @return
     */
    public List<RtxErpConfig> validationErpConfig(String whseid,String werks,String lgort) {
    	LambdaQueryWrapper<RtxErpConfig> query = Wrappers.lambdaQuery();
    	query.eq(RtxErpConfig::getWerks, werks);
    	query.eq(RtxErpConfig::getLgort, lgort);
    	List<RtxErpConfig> list =	rtxErpConfigMapper.selectList(query);
    	if(CollectionUtils.isEmpty(list)) {
    		throw new RuntimeException("工厂-"+werks+"-库存地-"+lgort+"未在工厂库存地中维护!");
    	}
    	return list;
    }
    
    /**
     * 验证SKU
     * @param whseid
     * @param storerkey
     * @param sku
     * @return
     */
    @DataSourceAnnotation
    public Sku validationSku(String whseid,String storerkey,String sku,boolean isExists) {
        LambdaQueryWrapper<Sku> query = Wrappers.lambdaQuery();
        query.eq(Sku::getStorerkey,storerkey).eq(Sku::getSku,sku);
        Sku s = skuMapper.selectOne(query);
        if(isExists){
            Assert.notNull(s,"货主：" + storerkey + "，sku：" + sku + "不存在");
        }
        return s;
    }

    /**
     * 根据code,listname验证系统代码
     * @param whseid
     * @param code
     * @param listname
     * @return
     */
    @DataSourceAnnotation
    public Codelkup validationCodelkup(String whseid,String code,String listname,boolean isExists){
        LambdaQueryWrapper<Codelkup> query = Wrappers.lambdaQuery();
        query.eq(Codelkup::getCode,code).eq(Codelkup::getListname,listname);
        Codelkup codelkup = codelkupMapper.selectOne(query);
        if(isExists){
            Assert.notNull(codelkup,"代码：" + code + "，listname：" + listname + "不存在");
        }
        return codelkup;
    }
    
    public static void main(String[] args) {
    	Codelkup codelkup = null;
    	Assert.notNull(codelkup,"代码：，listname：不存在");
	}

    /**
             * 根据wrapper验证 验证系统代码,报错
     * @param whseid
     * @param wrapper
     * @param isExists
     * @return
     */
    @DataSourceAnnotation
    public Codelkup validationCodelkup(String whseid,Wrapper<Codelkup> wrapper, boolean isExists){
    	List<Codelkup> codelkupList = codelkupMapper.selectList(wrapper);
        if(isExists){
            Assert.notEmpty(codelkupList,"系统代码未配置");
        }
        return codelkupList.get(0);
    }
    
    @DataSourceAnnotation
    public Codelkup validationCodelkupWAREHOUSECODER(String whseid, LambdaQueryWrapper<Codelkup> wrapper, boolean isExists){
    	wrapper.eq(Codelkup::getListname,"RTXSHIPSTORE");
    	List<Codelkup> codelkupList = codelkupMapper.selectList(wrapper);
        if(isExists){
            Assert.notEmpty(codelkupList,"下传报文店仓编码在企业级中系统代码RTXSHIPSTORE中未配置匹配关系！");
        }
        return codelkupList.get(0);
    }

    /**
     * 验证仓库是否存在
     * @param whseid
     * @param dbloigid
     * @return
     */
    @DataSourceAnnotation
    public PlDb validationWhseidByDblogid(String whseid, String dbloigid){
        PlDb plDb = commonMapper.selectActiveWhseidByDblogid(dbloigid);
        Assert.notNull(plDb,"仓库：" + dbloigid + "仓库不存在或未激活");
        return plDb;
    }
}
